package saver;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import pojo.Book;
import dao.BookDao;
import dao.Impl.BookDaoImpl;

public class SaveToDB {
	private BookDao bookDao;
	
	public SaveToDB(){
		bookDao = new BookDaoImpl();
	}
	
	/**
	 * parse the json string into book object, then add to DB with batch process
	 * @param strings
	 * @return isSucceed
	 */
	public boolean save(String[] strings) {
		Book[] books = new Book[1000];
		ObjectMapper mapper = new ObjectMapper();
		
		for(int i=0; i<strings.length; i++){
			Book book = null;
			try {
				book = mapper.readValue(strings[i], Book.class);
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
			books[i] = book;
		}
		
		int len = ((BookDaoImpl)bookDao).addBook2(books);
		
		if(len == books.length){
			return true;
		}else{
			return false;
		}
		
	}
}
